#version 120
uniform sampler2D src;
uniform sampler2D diffuse;
uniform float dsizeX, dsizeY;
void main()
{
	vec4 rs = vec4(0.0,0.0,0.0,1.0);
	float sum = 0.0; 
	if (texture2D(diffuse, gl_TexCoord[0].xy).xyz != vec3(0.0,0.0,0.0))
	{
		rs = vec4(texture2D(src, gl_TexCoord[0].xy).xyz,1.0);
		sum = 1.0;
	}
	vec3 v = rs.xyz;
	vec2 texCoord;
	
	texCoord = gl_TexCoord[0].xy+vec2(-dsizeX,-dsizeY);
	if (texture2D(diffuse, texCoord).xyz != vec3(0.0,0.0,0.0))
	{
		v += texture2D(src, texCoord).xyz;
		sum += 1.0;
	}
	texCoord = gl_TexCoord[0].xy+vec2(-dsizeX,dsizeY);
	if (texture2D(diffuse, texCoord).xyz != vec3(0.0,0.0,0.0))
	{
		sum += 1.0;
		v += texture2D(src, texCoord).xyz;
	}
	texCoord = gl_TexCoord[0].xy+vec2(dsizeX,dsizeY);
	if (texture2D(diffuse, texCoord).xyz != vec3(0.0,0.0,0.0))
	{
		v += texture2D(src, texCoord).xyz;
		sum += 1.0;
	}
	texCoord = gl_TexCoord[0].xy+vec2(dsizeX,-dsizeY);
	if (texture2D(diffuse, texCoord).xyz != vec3(0.0,0.0,0.0))
	{
		sum += 1.0;
		v += texture2D(src, texCoord).xyz;
	}
	
	texCoord = gl_TexCoord[0].xy+vec2(-dsizeX,0);
	if (texture2D(diffuse, texCoord).xyz != vec3(0.0,0.0,0.0))
	{
		sum += 1.0;
		v += texture2D(src, texCoord).xyz;	
	}
	texCoord = gl_TexCoord[0].xy+vec2(0,dsizeY);
	if (texture2D(diffuse, texCoord).xyz != vec3(0.0,0.0,0.0))
	{
		sum += 1.0;
		v += texture2D(src, texCoord).xyz;
	}
	texCoord = gl_TexCoord[0].xy+vec2(dsizeX,0);
	if (texture2D(diffuse, texCoord).xyz != vec3(0.0,0.0,0.0))
	{
		sum += 1.0;
		v += texture2D(src, texCoord).xyz;
	}
	texCoord = gl_TexCoord[0].xy+vec2(0,-dsizeY);
	if (texture2D(diffuse, texCoord).xyz != vec3(0.0,0.0,0.0))
	{
		sum += 1.0;
		v += texture2D(src, texCoord).xyz;
	}
	v /= sum;
	
	rs.xyz = v;
	gl_FragColor = rs;
}